module.exports = function ($scope, $state, $resource, $modal,getDate, toaster) {
	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadlist();
		}
	};
	$scope.pageInfo = {};
	$scope.pageInfo.pageNo = 1;		//当前页码
	$scope.pageInfo.pageSize = 10;	//每页显示几条
	$scope.pageInfo.totalItems = 0;	//总条数
	$scope.searchform = {'moreSearch': false};

	$scope.date = {
        'lable': null,
        'opened': false
    }

    $scope.date1 = {
        'lable': null,
        'opened': false
	}
	
	$scope.startDate = {
		'value': '',
		'opened': false
	}
	$scope.endDate = {
		'value': '',
		'opened': false
	}
	$scope.dateOpen = function ($event, item) {
		$event.preventDefault();
		$event.stopPropagation();
		item.opened = true;
	}
	function date2str(d) {
		if (d === undefined) {
			return "";
		}
		var month = (d.getMonth() + 1).toString();
		var day = d.getDate().toString();
		if (month.length == 1) month = '0' + month;
		if (day.length == 1) day = '0' + day;
		return d.getFullYear() + "-" + month + "-" + day;
	}

	//卡列表
	$scope.loadlist = function () {

		// $scope.pageInfo.start_time = (typeof $scope.startDate.value === 'string') ? $scope.startDate.value : date2str($scope.startDate.value);
		// $scope.pageInfo.end_time = (typeof $scope.endDate.value === 'string') ? $scope.endDate.value : date2str($scope.endDate.value);

		$scope.pageInfo.start_time = getDate($scope.date.lable);
		$scope.pageInfo.end_time =getDate($scope.date1.lable);
		$resource('/api/as/mc/shoporder/findOrderList', {}, {}).save($scope.pageInfo, function (res) {
			if (res.errcode === 0) {
				$scope.beanList = res.data.results;
				$scope.pageInfo.totalItems = res.data.totalRecord;
			} else {
				toaster.error({ title: '', body: res.errmsg });
			}
		});
	}

	$scope.loadlist();

	//详情
	$scope.info = function (bean) {
		var modalInstance = $modal.open({
			template: require('../views/skwxOrderInfo.html'),
			controller: 'skwxOrderInfo',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			// $scope.loadlist();
		});
	}

	//详情
	$scope.profit = function (bean) {
		var modalInstance = $modal.open({
			template: require('../views/skwxShopOrderProfit.html'),
			controller: 'skwxShopOrderProfit',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			// $scope.loadlist();
		});
	}

	$scope.moreSearch = function(){
        $scope.searchform.moreSearch = !$scope.searchform.moreSearch;
	}
	//----以下是页面快捷选择日期按钮-----
    $scope.today = function(){
        var now = new Date();
        $scope.date.lable = getDate(now);
        $scope.date1.lable = $scope.date.lable;
    }

    $scope.yesterday = function(){
        var now = new Date();
        now.setDate(now.getDate() - 1);
        $scope.date.lable = getDate(now);
        $scope.date1.lable = getDate(now);
    }

    $scope.thisWeek = function(){
        var now = new Date();
        $scope.date1.lable = getDate(now);

        var week = now.getDay() - 1;
        week = week == -1 ? 6 : week
        now.setDate(now.getDate() - week);
        $scope.date.lable = getDate(now);
    }

    $scope.lastWeek = function(){
        var now = new Date();
        var week = now.getDay() - 1;
        week = week == -1 ? 6 : week
        now.setDate(now.getDate() - week);
        $scope.date1.lable = getDate(now);

        now.setDate(now.getDate() - 7);
        $scope.date.lable = getDate(now);
    }

    $scope.thisMonth = function(){
        var now = new Date();
        $scope.date1.lable = getDate(now);
        
        now.setDate(1);
        $scope.date.lable = getDate(now);
    }

    $scope.lastMonth = function(){
        var now = new Date();
        now.setDate(0);
        $scope.date1.lable = getDate(now);

        now.setDate(1);
        $scope.date.lable = getDate(now);
    }
};